package com.foreveross.bsl.mam.app.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.foreveross.bsl.mam.app.application.AppVersionWidgetVersionApplication;
import com.foreveross.bsl.mam.app.application.vo.AppVersionWidgetVersionVo;
import com.foreveross.bsl.mam.model.application.WidgetApplication;
import com.foreveross.bsl.mam.model.application.WidgetVersionApplication;
import com.foreveross.bsl.mam.model.application.vo.WidgetVO;
import com.foreveross.bsl.mam.model.application.vo.WidgetVersionVO;

/**
 * 应用类别操作类
 * 
 * @author tanzhixiong
 * @2013-6-22 @下午3:01:42
 */
@Controller
@RequestMapping("/mam/appwidgets")
public class AppVersionWidgetVersionController {

	@Inject
	private AppVersionWidgetVersionApplication appVersionWidgetVersionApplication;

	@Inject
	private WidgetVersionApplication widgetVersionApplication;

	@Inject
	private WidgetApplication widgetApplication;


	private List<AppVersionWidgetVersionVo> convertJsonToObjList(
			List<Map<String, String>> relations) {
		List<AppVersionWidgetVersionVo> appVersionWidgetVersionVos = new ArrayList<AppVersionWidgetVersionVo>();
		// 把json转换成List<AppVersionWidgetVersionVo>
		for (int i = 0; i < relations.size(); i++) {
			AppVersionWidgetVersionVo vo = new AppVersionWidgetVersionVo();
			vo.setAppId(relations.get(i).get("appId"));
			vo.setAppVersion(relations.get(i).get("appVersion"));
			vo.setWidgetId(relations.get(i).get("widgetId"));
			vo.setWidgetVersion(relations.get(i).get("widgetVersion"));
			vo.setPlatform(relations.get(i).get("platform"));
			appVersionWidgetVersionVos.add(vo);
		}
		return appVersionWidgetVersionVos;
	}

	@ResponseBody
	@RequestMapping(value = "/{platform}/relations", method = RequestMethod.POST)
	public Map<String, String> addMultiRelations(
			@RequestBody List<Map<String, String>> relationships) {
		Map<String, String> result = new HashMap<String, String>();
		List<AppVersionWidgetVersionVo> appVersionWidgetVersionVos = convertJsonToObjList(relationships);
		appVersionWidgetVersionApplication
				.saveEntities(appVersionWidgetVersionVos);
		result.put("result", "success");
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/{platform}/relations", method = RequestMethod.PUT)
	public Map<String, String> updateWidget(
			@RequestBody Map<String, String> relationships) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			// 根据应用id应用版本和模块id查询关联对象
			AppVersionWidgetVersionVo entity = appVersionWidgetVersionApplication
					.getAppWidgetEntity(relationships.get("appId"),
							relationships.get("platform"),
							relationships.get("appVersion"),
							relationships.get("widgetId"));
			if (entity != null) {
				entity.setWidgetVersion(relationships.get("widgetVersion"));
				appVersionWidgetVersionApplication.updateEntity(entity);
				result.put("result", "success");
			} else {
				result.put("result", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/{platform}/relations", method = RequestMethod.DELETE)
	public Map<String, Object> deleteMultiRelations(
			@RequestBody List<Map<String, String>> relationships) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<AppVersionWidgetVersionVo> relationsToDelete = convertJsonToObjList(relationships);
		try {
			appVersionWidgetVersionApplication
					.deleteEntities(relationsToDelete);
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}

	/**
	 * 此方法描述的是：关联模块
	 * 
	 * @author: panhm@foreveross.com
	 * @version: 2013-8-23 下午6:42:47
	 */

	@RequestMapping(value = "/{platform}/{version}/relations/{appId}", method = RequestMethod.GET)
	public String appWidget(@PathVariable("platform") String platform,
			@PathVariable("version") String version,
			@PathVariable("appId") String appId, Model model) {
		// 获取已关联模块
		List<WidgetVO> listWidget = new ArrayList<WidgetVO>();
		// AppVersionVo versionVo = appVersionApplication.getEntity(appVId);
		List<AppVersionWidgetVersionVo> list = appVersionWidgetVersionApplication
				.getListByPlatEntity(platform, version, appId);
		for (AppVersionWidgetVersionVo appWidget : list) {
			if (appWidget.getWidgetId() != null) {
				WidgetVO widget = widgetApplication.getEntity(appWidget
						.getWidgetId());
				if (widget != null) {
					List<WidgetVersionVO> listVersion = widgetVersionApplication
							.findByWidgetId(widget.getId());
					if (listVersion != null && listVersion.size() > 0) {
						widget.setwVersion(listVersion);
						widget.setDiscription(appWidget.getWidgetVersion());
						listWidget.add(widget);
					}
				}
			}
		}
		// 获取未关联模块
		List<WidgetVO> resultList = new ArrayList<WidgetVO>();
		List<WidgetVO> listAll = widgetApplication.findAll();
		if (listWidget != null && listWidget.size() > 0) {
			boolean nolinked = true;
			for (WidgetVO cloud : listAll) {// 遍历所有模块
				nolinked = true;
				for (WidgetVO versionWidget : listWidget) { // 遍历已经关联模块
					if (cloud.getId().equals(versionWidget.getId())) {
						nolinked = false;
						break;
					}
				}
				if (nolinked) {// 如果没有关联，则添加到未关联集合中
					List<WidgetVersionVO> wVersion = widgetVersionApplication
							.findByWidgetId(cloud.getId());
					if (wVersion != null && wVersion.size() > 0) {
						cloud.setwVersion(wVersion);
						resultList.add(cloud);
					}
				}
			}
		} else {
			resultList.addAll(listAll);
		}
		model.addAttribute("platform", platform);
		model.addAttribute("versions", version);
		model.addAttribute("appId", appId);
		model.addAttribute("linkWidget", listWidget);
		model.addAttribute("noLinkWidget", resultList);
		return "app/showWiget";
	}

	@ResponseBody
	@RequestMapping(value = "/relations/{version}/{appId}", method = RequestMethod.DELETE)
	public Map<String, Object> deleteWidgetRelations(
			@PathVariable("version") String version,
			@PathVariable("appId") String appId) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			appVersionWidgetVersionApplication.deleteByWidgetRelation(version,
					appId);
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delrelations/{platform}/{appVersion}/{appId}/{widgetVersion}/{widgetId}", method = RequestMethod.DELETE)
	public Map<String, Object> deleteWidgetByVersion(
			@PathVariable("platform") String platform,
			@PathVariable("appVersion") String appVersion,
			@PathVariable("appId") String appId,
			@PathVariable("widgetVersion") String widgetVersion,
			@PathVariable("widgetId") String widgetId) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			appVersionWidgetVersionApplication.delByVersion(platform,appVersion,appId,widgetVersion,widgetId);
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}
}
